//
//  THJMyFabuListVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2018/10/10.
//  Copyright © 2018年 河南德艺. All rights reserved.
//

#import "THJMyFabuListVC.h"
#import "THJMyFabuListCell.h"
#import "THJFabuDianjiaVC.h"


@interface THJMyFabuListVC ()<UITableViewDelegate,UITableViewDataSource,THJFabudianjiaDelegate,THJFabuDianjiaVCDelegate>
{
    UITableView * _tableview;
    NSInteger btnIndex;
    NSArray * dataArr;
    NSString * act;
}
@property (weak, nonatomic) IBOutlet UIView *lineView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *lineViewLeftSpace;

@end

@implementation THJMyFabuListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    btnIndex = 1;
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"我的发布"];
    }else{
        [self setNavBarWithTitle:@"我的发布"];
    }
    self.topSpace.constant = NavbarHeight;
    act = @"buy";
    [self getDataWithAct:act];
    _tableview = [self setTableviewWithFrame:CGRectMake(0, NavbarHeight+42, ScreenWidth, ScreenHeight - NavbarHeight - 42) RegisterNibNameArr:@[@"THJMyFabuListCell"] ReuseIdentifierArr:@[@"cell"] RowHeight:140 BgColor:nil IsLoadMore:NO];
    // Do any additional setup after loading the view from its nib.
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return dataArr.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"indexpath---%ld",indexPath.row);
    THJMyFabuListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.delegate = self;
    [cell.editBtn setTag:indexPath.row + 600];
    NSDictionary * dic = dataArr[indexPath.row];
    [cell setContentWithDic:dic Flag:btnIndex];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)selectIndex:(NSInteger)index{
    NSLog(@"index--%ld",index);
    NSDictionary * dic = dataArr[index];
    //初始化一个UIAlertController的警告框
    UIAlertController *alertController = [[UIAlertController alloc] init];
    //初始化一个UIAlertController的警告框将要用到的UIAlertAction style cancle
    UIAlertAction *cancle = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        NSLog(@"t提示框上的按钮 cancle 被点击了");
    }];
    //初始化一个UIAlertController的警告框将要用到的UIAlertAction style Default
    UIAlertAction *wx = [UIAlertAction actionWithTitle:@"编辑" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        THJFabuDianjiaVC * vc = [THJFabuDianjiaVC new];
        vc.fromFlag = @"edit";
        vc.itemid = dic[@"id"];
        vc.act = btnIndex == 1 ? @"buy":@"sell";
        [self.navigationController pushViewController:vc animated:YES];
    }];
    //初始化一个UIAlertController的警告框将要用到的UIAlertAction style Default
    UIAlertAction *wb = [UIAlertAction actionWithTitle:@"上架" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"t提示框上的按钮 微博分享 被点击了");
        [self refreshActionWithIndexpath:index];
    }];
    UIAlertAction *xiajia = [UIAlertAction actionWithTitle:@"下架" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
        NSLog(@"t提示框上的按钮 微博分享 被点击了");
        [self xiajiaActionWithIndexpath:index];
    }];
    //将初始化好的UIAlertAction添加到UIAlertController中
    [alertController addAction:cancle];
    [alertController addAction:wx];
    [alertController addAction:wb];
    [alertController addAction:xiajia];
    //将初始化好的j提示框显示出来
    [self presentViewController:alertController animated:true completion:nil];
    
}
-(void)xiajiaActionWithIndexpath:(NSInteger)indexpath{
    NSDictionary * dic = dataArr[indexpath];
    NSString * itemid = dic[@"id"];
    NSString * act = btnIndex == 1 ? @"buy":@"sell";
    NSString * url = [NSString stringWithFormat:@"https://dianjia.thjb.net/dingjia.php?f=Member&r=Member&m=xia&act=%@&id=%@",act,itemid];
    [THJNetWork getNetDataWithTokenFromUrl:url callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"下架:%@",receiveData);
        if([receiveData[@"status"] isEqual:@"200"]){
            [self HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
            [self getDataWithAct:act];
        }
    }];
}
-(void)refreshActionWithIndexpath:(NSInteger)indexpath{
    NSDictionary * dic = dataArr[indexpath];
    NSString * itemid = dic[@"id"];
    NSString * act = btnIndex == 1 ? @"buy":@"sell";
    NSString * url = [NSString stringWithFormat:@"https://dianjia.thjb.net/dingjia.php?f=Member&r=Member&m=productlist&act=%@&id=%@",act,itemid];
    [THJNetWork getNetDataWithTokenFromUrl:url callback:^(NSDictionary* receiveData, NSError *error) {
        NSLog(@"刷新:%@",receiveData);
        if([receiveData[@"status"] isEqual:@"200"]){
            [self HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
            [self getDataWithAct:act];
        }
    }];
}
-(void)getDataWithAct:(NSString*)act{
    [self SVHUDShow];
    NSString * url  = [NSString stringWithFormat:@"https://dianjia.thjb.net/dingjia.php?f=Member&r=Member&m=productlist&act=%@",act];
    [THJNetWork getNetDataWithTokenFromUrl:url callback:^(NSDictionary * receiveData, NSError *error) {
        [self SVHUDHiden];
        
        NSLog(@"&&&%@",receiveData[@"data"]);
        dataArr = receiveData[@"data"];
        [_tableview reloadData];
    }];
}
- (IBAction)btn1Action:(id)sender {
    NSLog(@"%ld",btnIndex);
    if(btnIndex == 1){
        NSLog(@"001");
        return;
    }else{
        NSLog(@"001--");
        btnIndex = 1;
        act = @"buy";
        self.lineViewLeftSpace.constant = 0;
        [self getDataWithAct:act];
    }
}
- (IBAction)btn2Action:(id)sender {
    NSLog(@"%ld",btnIndex);
    if(btnIndex == 2){
        NSLog(@"002");
        return;
    }else{
        NSLog(@"002--");
        btnIndex = 2;
        act = @"sell";
        self.lineViewLeftSpace.constant = ScreenWidth/2;
        [self getDataWithAct:act];
    }
}
// 编辑成功之后 协议回调刷新当前列表
-(void)oprateSuccessPara:(id)para{
    [self getDataWithAct:act];
}
@end
